package models

import (
	"context"
	"encoding/json"
	"errors"
	"github.com/cy77cc/hioshop/global"
	"gorm.io/gorm"
	"time"
)

type Model struct {
	ID        uint           `json:"id" gorm:"primarykey"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

type Ad struct {
	Model
	LinkType  uint   `json:"link_type" gorm:"type:tinyint;default:0"`
	Link      string `json:"link" gorm:"varchar(255);default:''"`
	GoodsId   int    `json:"goods_id" gorm:"type:int;default:0"`
	ImageUrl  string `json:"image_url" gorm:"text;"`
	EndTime   int    `json:"end_time" gorm:"type:int;default:0"`
	Enabled   uint8  `json:"enabled" gorm:"type:tinyint unsigned;default:0"`
	SortOrder int8   `json:"sort_order" gorm:"type:tinyint;default:0"`
	IsDelete  int8   `json:"is_delete" gorm:"type:tinyint;default:0"`
}

func (a *Ad) GetAds() (list []Ad, err error) {
	get := global.REDIS.Get(context.Background(), "ad")
	if get.Err() != nil {
		err = global.DB.Model(&Ad{}).
			Where("enabled=1 and is_delete=0").Order("sort_order desc").Find(&list).Error

		if errors.Is(err, gorm.ErrRecordNotFound) {
			err = global.REDIS.Set(context.Background(), "ad", "", time.Hour).Err()
		}
		data, _ := json.Marshal(list)
		err = global.REDIS.Set(context.Background(), "ad", data, time.Hour).Err()
		//fmt.Println(push.Err())
		return
	}

	data, _ := get.Result()
	err = json.Unmarshal([]byte(data), &list)

	return
}
